<div id="existing_image_area" class="dialog_area" <%= 'style="display:none;"'.html_safe if @image.errors.any? %>>
  <%= render '/refinery/admin/search', :url => refinery.insert_admin_images_path(params.dup.except('image')) %>
  <input type="hidden" name="selected_image" id="selected_image" />
  <div id="existing_image_area_content" class="clearfix">
<% if @images.any? %>
    <ul>
      <% @images.each do |image| -%>
        <li<%= ' class="selected"'.html_safe if @image_id == image.id %>>
          <%= image_fu(image, '106x106#c', {
                         :alt => image.title,
                         :title => image.title,
                         :id => "image_#{image.id}",
                         :'data-id' => image.id
                       }.merge(thumbnail_urls(image))) -%>
        </li>
      <% end -%>
    </ul>
<% elsif searching? %>
    <%= t('no_results', :scope => 'refinery.admin.search') %>
<% end %>
  </div>
  <%= will_paginate @images, :params => params.dup.merge(:action => "insert") %>

  <% unless @app_dialog or @images.empty? %>
    <div id="existing_image_size_area" class="clearfix">
      <input type="hidden" name="selected_image_size" id="selected_image_size" />
      <p>
        <input type="checkbox" id="wants_to_resize_image" name="wants_to_resize_image" value="1" checked="checked" />
        <label for="wants_to_resize_image" class="stripped" style="font-weight: bold;">
          <%= t('.resize_image') %>
        </label>
      </p>
      <ul>
        <%
          Refinery::Images.user_image_sizes.sort_by { |key, geometry| geometry.to_i }.each_with_index do |(size, pixels), index|
            safe_pixels = pixels.to_s.gsub(/[<>=]/, '')
            # (parndt): ' selected' if size.to_s == 'medium' is not very generic, but I
            # can't think of a decent way of making it so for even sets (e.g. 2,4,6,8,etc image sizes).
        -%>
            <li id="image_dialog_size_<%= index %>" class="image_dialog_size<%= ' selected' if size.to_s == 'medium' %>">
              <%= link_to size.to_s, "##{size}",
                          :'data-geometry' => pixels,
                          :'data-size' => size.to_s.parameterize,
                          :title   => "#{size} image (#{safe_pixels})",
                          :tooltip => "#{size} image (#{safe_pixels})" %>
            </li>
        <% end -%>
      </ul>
    </div>
  <% end %>

  <%= render '/refinery/admin/form_actions', :f => nil,
             :cancel_url => '',
             :submit_button_text => t('.button_text'),
             :hide_submit => @images.empty?,
             :hide_cancel => false,
             :hide_delete => true,
             :cancel_title => nil if @app_dialog or @images.any? or searching? %>
</div>
